package com.misty.blog.aop;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.misty.blog.service.BlogService;
import com.misty.common.Util.RedisCache;
import com.misty.dao.domain.Blog;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.CodeSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @ClassName : ViewStatistics
 * @Description :
 * @Author :
 * @Date: 2022-12-12 23:01
 */
@Aspect
@Component
@Slf4j
public class ViewStatisticsAspect {

    @Autowired
    private BlogService blogService;

    @Autowired
    private RedisCache redisCache;

    @Pointcut("@annotation(com.misty.blog.annotation.ViewStatistics)")
    public void viewPointCut() { }

    @Before(value="viewPointCut()")
    public void viewStatisticsIntercept( JoinPoint joinPoint) {
        log.info("============== 用户进入浏览博客 =====================");

//        //获取接口的参数值
        Object[] args = joinPoint.getArgs();
//        JSONObject jsonObject = null;
        String[] parameterNames = ((CodeSignature) joinPoint.getSignature()).getParameterNames();
        Integer blogId = null;
        log.info("============{}=============",parameterNames);

//          获取浏览的博客ID
        for(int i=0;i<parameterNames.length;i++){
            String paramName = parameterNames[i];
            if(paramName.equals("id")){
                //获取token来源
                blogId = (Integer) args[i];
                log.info("============== 用户进入浏览id为 {} 博客 =====================",blogId);
            }
        }
        if(redisCache.exists("view" + blogId.toString()) && !redisCache.isExpire("view" + blogId.toString())){
            return ;
        }else {
            redisCache.deleteObject("view" + blogId.toString());
            //blogID,存入redis,30分钟内做幂等
            redisCache.setCacheObject("view" + blogId.toString(),blogId,30, TimeUnit.MINUTES);
        }

        //浏览量+1
        Blog blog = new Blog();
        UpdateWrapper<Blog> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(Blog::getId,blogId)
                .setSql("views = views + 1");
        boolean update = blogService.update(updateWrapper);
        if(!update){
            log.info("更新{}浏览量成功！",blogId);
        }
//
//        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
//        Method method = signature.getMethod();
//        AppAuthenticationValidate annotation = method.getAnnotation(AppAuthenticationValidate.class);
//        String[] requestParams = annotation.requestParams();
    }



    @After(value = "viewPointCut()")
    public void after(JoinPoint joinPoint) {
        log.info("============== 用户浏览博客结束 =====================");
    }
//    @Around("viewPointCut()")
//    public Object intercept(ProceedingJoinPoint joinPoint) throws Throwable {
//        return null;
//    }
//
//    @AfterReturning("viewPointCut()")
//    public void afterReturning(JoinPoint joinPoint) {
//
//    }
//
//    @AfterThrowing("viewPointCut()")
//    public void afterThrowing(JoinPoint joinPoint) {
//
//    }
}
